package com.example.mengchun;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mengchun.Adapter.DraAdapter;
import com.example.mengchun.viewpager.Viewpager;
import com.example.mengchun.vo.Darmodel;

import org.feezu.liuli.timeselector.TimeSelector;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Darmodel> mList = new ArrayList<Darmodel>();
    private ListView mListView;
    private ListView mListView1;
    private GridView gridView;
    int []beijin = {R.drawable.xingzuo0,R.drawable.xingzuo1
            ,R.drawable.xingzuo2,R.drawable.xingzuo3
            ,R.drawable.xingzuo4,R.drawable.xingzuo5
            ,R.drawable.xingzuo6,R.drawable.xingzuo7
            ,R.drawable.xingzuo8,R.drawable.xingzuo9
            ,R.drawable.xingzuo10,R.drawable.xingzuo11};
    private int []im = {R.drawable.xingz0,R.drawable.xingz1,
            R.drawable.xingz2,R.drawable.xingz3,
            R.drawable.xingz4,R.drawable.xingz5,
            R.drawable.xingz6,R.drawable.xingz7,
            R.drawable.xingz8,R.drawable.xingz9,
            R.drawable.xingz10,R.drawable.xingz11,};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mutil.transparentStatusBar(this);
        setContentView(R.layout.activity_main);
        final DrawerLayout mdrawerlayout = (DrawerLayout) findViewById(R.id.mamengchundra);
        findViewById(R.id.leftmenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdrawerlayout.openDrawer(Gravity.LEFT);//左菜单的打开
            }
        });
        initData();
        findViewById(R.id.user_iid).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdrawerlayout.openDrawer(Gravity.RIGHT);
            }
        });
        mListView1 = (ListView) findViewById(R.id.list_iteem);
        mListView1.setAdapter(new MyAdapter());
        mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("用户星座："+getSharedPreferences("user_date",MODE_PRIVATE)
                                        .getString("xingzuo",""))
                                .setMessage("用户星座："+getSharedPreferences("user_date",MODE_PRIVATE)
                                        .getString("date",""))
                                .show();
                        break;
                    case 1:
                        new AlertDialog.Builder(MainActivity.this).setTitle("关于")
                                .setMessage("检查更新")
                                .setPositiveButton("检查更新",null)
                                .setNegativeButton("取消",null)
                                .show();
                        break;
                    case 2:
                        TimeSelector timeSelector = new TimeSelector(MainActivity.this, new TimeSelector.ResultHandler() {
                            @Override
                            public void handle(String time) {
                                SharedPreferences s = getSharedPreferences("user_date",MODE_PRIVATE);
                                SharedPreferences.Editor e = s.edit();
                                e.putString("date",time);
                                e.putString("xingzuo",Mutil.xingzuo(time.substring(5,7),time.substring(8,10)));
                                e.commit();
                                for (int a = 0;a<xingzuoz.length;a++){
                                    if (xingzuoz[a].trim().equals(Mutil.xingzuo(time.substring(5,7),time.substring(8,10)).trim().substring(0,2).trim())){
                                        ImageView i = (ImageView) findViewById(R.id.touxia);
                                        i.setImageResource(im[a]);

                                    }
                                }
                                Toast.makeText(MainActivity.this, Mutil.xingzuo(time.substring(5,7),time.substring(8,10)), Toast.LENGTH_SHORT).show();
                            }
                        }, "1994-01-01 00:00", "2017-12-31 23:59:59");

                        timeSelector.show();
                        break;
                }
            }
        });

        final int[] a = {20};
        mListView = (ListView) findViewById(R.id.left_listview);
        DraAdapter adapter = new DraAdapter(this,mList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 1:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 2:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 3:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 4:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 5:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 6:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 7:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 8:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 9:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 10:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 11:
                        gotoss(mdrawerlayout,position);
                        break;
                    case 12:
                        gotoss(mdrawerlayout,position);
                        break;
                }
            }
        });
        gridView = (GridView) findViewById(R.id.gri_xing);

        BaseAdapter baseAdapter = new BaseAdapter() {

            @Override
            public int getCount() {
                return im.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = getLayoutInflater().inflate(R.layout.grid_xing,null);
                ImageView imageView = (ImageView) convertView.findViewById(R.id.imag_xing);
                imageView.setImageResource(im[position]);
                return convertView;
            }
        };
        gridView.setAdapter(baseAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gotoss(mdrawerlayout,position);
            }
        });

    }
    FragmentTransaction transaction;
    public void replaceFragment(Fragment fragment){
        //1、拿到FragmentManager管理器
        FragmentManager manager = getSupportFragmentManager();

        //2、获取事物
        transaction = manager.beginTransaction();

        //3、fragment的替换
        transaction.replace(R.id.content,fragment);

        //4、提交事物
        transaction.commit();
    }
    private Xingzuo_m xingzzz;
    public void gotoss(final DrawerLayout mdrawerlayout, final int position){
        Mutil.gethttpconnec("http://114.67.157.123/servlet/Fanhui?xingzuo="+position+"",new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                xingzzz = new Xingzuo_m(msg.obj.toString(),position);
                replaceFragment(xingzzz);
                mdrawerlayout.closeDrawer(Gravity.LEFT);
                gridView.setVisibility(View.GONE);
            }
        });
    }
    String xingzuoz[] = {"处女","金牛","巨蟹","摩羯","白羊","射手","狮子","双鱼","双子","水瓶","天枰","天蝎"};
    int xingzuot[] = {R.drawable.chunv,R.drawable.jinniu,R.drawable.juxie,R.drawable.mojie,R.drawable.muyang
            ,R.drawable.sheshou,R.drawable.shizi,R.drawable.shuangyu,R.drawable.shuangzi,R.drawable.shuiping
            ,R.drawable.tianping,R.drawable.tianxie};
    private void initData() {


        String xz = getSharedPreferences("user_date",MODE_PRIVATE).getString("xingzuo","").trim().substring(0,2);
        for (int a = 0;a<xingzuoz.length;a++){
            if (xingzuoz[a].trim().equals(xz.trim())){
                ImageView i = (ImageView) findViewById(R.id.touxia);
                i.setImageResource(im[a]);
                findViewById(R.id.fraglayout).setBackgroundResource(beijin[a]);
            }
        }
        for(int i = 0;i<12;i++){
            mList.add(new Darmodel(xingzuot[i],xingzuoz[i],i+1));
        }
    }
    class MyAdapter extends BaseAdapter{
        String []pp = {"用户","关于","设置"};
        @Override
        public int getCount() {
            return pp.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout ll = new LinearLayout(MainActivity.this);
            TextView t = new TextView(MainActivity.this);
            t.setText(pp[position]);
            t.setTextSize(30f);
            t.setTextColor(Color.parseColor("#000000"));
            ll.addView(t);
            return ll;
        }
    }
    int a = 0;
    @Override
    public void onBackPressed() {
        if (a == 0){
            gridView.setVisibility(View.VISIBLE);
            transaction.remove(xingzzz);
            a++;
        }else if(a == 1){
            super.onBackPressed();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                a --;
            }
        },2000);
    }
}
